package cn.qixin.passport.controller;


import cn.qixin.passport.pojo.dto.UserAddNewDTO;
import cn.qixin.passport.pojo.dto.UserLoginDTO;
import cn.qixin.passport.pojo.dto.UserUpdateDTO;
import cn.qixin.passport.pojo.vo.UserListItemVO;
import cn.qixin.passport.pojo.vo.UserStandardVO;
import cn.qixin.passport.service.IUserService;
import cn.qixin.passport.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "01. 用户管理模块")
@RequestMapping("/users")
public class UserController {

    @Autowired
    IUserService userService;
    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 50)
    @PostMapping("/login")
    public JsonResult<String> login(UserLoginDTO userLoginDTO){
        log.debug("开始处理【用户登录】的请求，参数：{}", userLoginDTO);
        String jwt = userService.login(userLoginDTO);
        return JsonResult.ok(jwt);
    }

    @ApiOperation("注册用户")
    @ApiOperationSupport(order = 100)
   // @PreAuthorize("hasAuthority('/qixin/user/add-new')")
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Valid UserAddNewDTO userAddNewDTO) {
        log.debug("开始处理【注册用户】的请求，参数：{}", userAddNewDTO);
        userService.addNew(userAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除用户")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAuthority('/qixin/user/delete')")
    @PostMapping("/{id:[1-9]+}/delete")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "long")
    public JsonResult<Void> deleteById(@PathVariable Long id){
        log.debug("开始处理【删除用户】的请求，参数：{}", id);
        userService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据id查询用户详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<UserStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询用户详情】的请求，参数：{}", id);
        UserStandardVO user = userService.getStandardById(id);
        return JsonResult.ok(user);
    }

    @ApiOperation("修改用户详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public  JsonResult<Void> update(@PathVariable Long id, UserUpdateDTO userUpdateDTO) {
        log.debug("开始处理【修改用户详情】的请求，参数ID：{}, 新数据：{}", id, userUpdateDTO);
        userService.update(id, userUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("显示用户列表")
    @ApiOperationSupport(order = 403)
    @PreAuthorize("hasAuthority('/qixin/user/read')")
    @GetMapping("")
    public JsonResult<List<UserListItemVO>> list(){
        log.debug("开始处理【查询用户列表】的请求");
        List<UserListItemVO> list = userService.list();
        return JsonResult.ok(list);
    }



}
